package org.kmp.project.main.home

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.kmp.project.net.api.getUserInfo

class HomeViewModel{
    var message by mutableStateOf("欢迎来到首页")
        private set
    
    var clickCount by mutableStateOf(0)
        private set
    
    fun updateMessage(newMessage: String) {
        message = newMessage
    }
    
    fun incrementClickCount() {
        clickCount++
        message = "按钮已点击 $clickCount 次"
    }


    private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)


    var userInfo by mutableStateOf("")
        private set

    fun loadData() {
        scope.launch {
            userInfo=""
            userInfo = try {
                delay(1000)
                val response = getUserInfo()
                "请求成功: ${response.title}"
            } catch (e: Exception) {
                println("请求失败: ${e.message}")
                "请求失败: ${e.message}"
            }
        }
    }
}